<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use vquery;
use app\model\zhanghao as zh;
use app\model\users as us;

/**
 * 分配权限
 *
 * @author Administrator
 */
class Auth extends Check {

    public function __construct() {
        parent::__construct();
        $left = controller("index");
        $left->left();
    }

    public function allocation() {
        //取出所有权限
        $uid = session("u_rid");
        $rlist = db("role")->select();
        $this->assign("rlist", $rlist);
//        $uid = session("u_id"); //取出用户ID
        $list = db("users")->alias("a")->join("sxd_role b", "a.u_rid=b.r_id")->paginate(30);
        $page = $list->render();
        $this->assign('page', $page);
        $this->assign("list", $list);
        $title = "Dengeki Bunko Allocation";
        session("cue", "分配权限");
        $this->assign("title", $title);
        $this->assign("uid", $uid);
        return $this->fetch();
    }

    //删除用户

    public function dodelete() {
        $id = input("ids");
        // $page = input("page");
        //$type = input("type");
        if (us::destroy($id)) {
            $this->redirect("/allocation");
        } else {
            return "<script>alert('删除失败,可能已经删除！');window.location.href='/allocation';</script>";
        }
    }

    /**
     * 修改用户权限
     */
    public function ajaxupdate() {


        $uid = input("uid"); //用户ID
        $rid = input("rid"); //权限ID
//        echo $rid;
        if (db("users")->where("u_id=$uid")->setField("u_rid", $rid)) {
            return ["status" => 1, 'message' => "分配成功！"];
        } else {
            return ["status" => 2, 'message' => "分配失败！"];
        }
    }

    /**
     * 修改用户状态（是否可以登录）
     */
    public function ajaxupdatestatus() {
        $uid = input("uid"); //用户ID
        if ($uid == 1) {
            return ["status" => 2, 'message' => "总管理员不能禁用！"];
        }
        $u_status = db("users")->where("u_id=$uid")->value("u_status");
//        $u_username = db("users")->where("u_id=$uid")->value("u_username");
//        if($u_username)
        //return $u_status;
        if ($u_status == 1) {
            db("users")->where("u_id=$uid")->setField("u_status", 2);
        } else {
            db("users")->where("u_id=$uid")->setField("u_status", 1);
        }
    }

}
